# MT::Plugin::MobilePublish
# Author: Takashi Ishikawa (http://www.ana-kutsu.com/)
# Copyright (C) 2011, Takashi Ishikawa.
#
# This program is distributed under the terms of the
# GNU General Public License, version 2.


package MT::Plugin::MobilePublish;

use strict;
use MT;
use MT::Plugin;
use MT::Template;

our $VERSION = '1.0';

use base qw(MT::Plugin);

@MT::Plugin::MobilePublish::ISA = qw(MT::Plugin);

my $objPlugin = new MT::Plugin::MobilePublish({
	id => 'MobilePublish',
	key => 'mobile_publish',
	description => '<MT_TRANS phrase=\'_PLUGIN_DESCRIPTION\'>',
	name => 'MobilePublish',
	author_name => 'Takashi Ishikawa',
	author_link => 'http://www.ana-kutsu.com/',
	version => $VERSION,
	schema_version => $VERSION,
	l10n_class => 'MobilePublish::L10N',
});

MT->add_plugin($objPlugin);

sub init_registry {
	my $objPlugin = shift;
	$objPlugin->registry({
		object_types => {
			'template' => {
				'carrier_template' => 'integer',
			},
			'entry' => {
				'carrier_entry' => 'integer',
			},
		},
		callbacks => {
			'build_file_filter' => \&build_file_filter,
			'MT::App::CMS::template_param.edit_template' => \&edit_template_param,
			'MT::App::CMS::template_param.edit_entry' => \&edit_entry_param,
		},
		tags => {
			'filters' => {
				'Entries' => {
					'publish_type' => {
						'handler' => \&publish_type,
					},
				},
			},
		},
	});
}

sub build_file_filter {
	my ($objCallback, %listParam) = @_;
	my $objEntry = $listParam{'Entry'};
	my $objTemplate = $listParam{'Template'};
	my $strArchiveType = $listParam{'ArchiveType'};
	my $flagResult = 1;
	
	if ($strArchiveType eq 'Page') {
		if (($objEntry->carrier_entry > 0) && ($objTemplate->carrier_template > 0) && ($objEntry->carrier_entry != $objTemplate->carrier_template)) {
			$flagResult = 0;
		}
	}
	
	return $flagResult;
}

sub edit_template_param {
	my ($objCallback, $objMt, $listParam, $objTemplate) = @_;
	my $objPointer = undef;
	my $objNode = undef;
	my $strForm = '';
	my $numCarrier = 0;
	my @listOption = (
		'PC/Mobile share',
		'PC only',
		'Mobile only',
	);
	
	if ($listParam->{'type'} eq 'page') {
		$objPointer = $objTemplate->getElementById('archive_mapping');
		$objNode = $objTemplate->createElement('app:setting', {
			id => 'mobile_publish',
			label => $objPlugin->translate('Type of publish'),
			label_class => 'top-label',
			required => 0
		});
		if ($listParam->{'carrier_template'}) {
			$numCarrier = $listParam->{'carrier_template'};
		}
		$strForm .= '<select id="carrier_template" name="carrier_template" style="width:25%" mt:watch-change="1">' . "\n";
		for (my $i = 0; $i <= $#listOption; $i++) {
			$strForm .= '<option value="' . $i . '"';
			if ($numCarrier == $i) {
				$strForm .= ' selected="selected"';
			}
			$strForm .= '>' . $objPlugin->translate($listOption[$i]) . '</option>' . "\n";
		}
		$strForm .= '</select>' . "\n";
		$objNode->innerHTML($strForm);
		$objTemplate->insertAfter($objNode, $objPointer);
	}
}

sub edit_entry_param {
	my ($objCallback, $objMt, $listParam, $objTemplate) = @_;
	my $objEntry = MT::Entry->load({id => $objMt->param->param('id')});
	my $objPointer = undef;
	my $objNode = undef;
	my $strForm = '';
	my $numCarrier = 0;
	my @listOption = (
		'PC/Mobile share',
		'PC only',
		'Mobile only',
	);
	
	if ($listParam->{'object_type'} eq 'page') {
		$objPointer = $objTemplate->getElementById('basename');
		$objNode = $objTemplate->createElement('app:setting', {
			id => 'mobile_publish',
			label => $objPlugin->translate('Type of publish'),
			label_class => 'top-label',
			required => 0
		});
		if ($objEntry && $objEntry->carrier_entry) {
			$numCarrier = $objEntry->carrier_entry;
		}
		$strForm .= '<select id="carrier_entry" name="carrier_entry" style="width:100%" mt:watch-change="1">' . "\n";
		for (my $i = 0; $i <= $#listOption; $i++) {
			$strForm .= '<option value="' . $i . '"';
			if ($numCarrier == $i) {
				$strForm .= ' selected="selected"';
			}
			$strForm .= '>' . $objPlugin->translate($listOption[$i]) . '</option>' . "\n";
		}
		$strForm .= '</select>' . "\n";
		$objNode->innerHTML($strForm);
		$objTemplate->insertAfter($objNode, $objPointer);
	}
}

sub publish_type {
	my ($objContext, $listParam) = @_;
	my $listFilter = $objContext->{'filters'};
	my @listType = ();
	my $numMask = 0;
	
	@listType = split(/,/, $listParam->{'publish_type'});
	foreach (@listType) {
		if ($_ eq 'share') {
			$numMask |= 1;
		} elsif ($_ eq 'pc') {
			$numMask |= 2;
		} elsif ($_ eq 'mobile') {
			$numMask |= 4;
		}
	}

	if ($numMask) {
		push(@{$listFilter}, sub {
			my $numResult = 0;
			
			if ($_[0]->{'carrier_entry'}) {
				$numResult = (2 ** $_[0]->{'carrier_entry'}) & $numMask;
			}
			
			return $numResult;
		});
	}
}

1;